package com.ymm.system.core.facade;

import com.ymm.driver.api.constants.Entity.DriverEntity;
import com.ymm.system.api.dto.*;
import com.ymm.system.api.dto.ShipperOrderBriefResponse;
import com.ymm.system.api.dto.ShipperOrderDetailResponse;
import com.ymm.system.api.facade.SystemOrderService;
import com.ymm.system.api.facade.SystemShipperOrderService;
import com.ymm.system.api.utils.*;
import com.ymm.system.core.constants.OpsPersonTypeEnum;
import com.ymm.system.core.repo.DriverRelatedRepo;
import com.ymm.system.dao.mapper.LltorderMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.Objects;

/**
 * @Author zikang.yao
 * @Description TODO
 * @Date 2022/8/4 21:48
 * @Version 1.0
 */
@Service(version = "server")
@Slf4j
@Component
public class SystemShipperOrderServiceImpl implements SystemShipperOrderService {


    @Autowired
    private DriverRelatedRepo driverRelatedRepo;


    @Resource
    private LltorderMapper lltorderMapper;


    @Resource
    private SystemOrderService systemOrderService;


    @Override
    public Result<Boolean> cancelOrderWithoutPullOffCargo(Long orderId, Long shipperId, Integer orderCancelReason, String orderCancelDescription) throws Exception {
        if (orderId == null || Objects.isNull(shipperId)) {
            log.info("参数输入错误");
            return Result.fail(false, ResultCodeEnum.PARAM_ERROR.getCode(), ResultCodeEnum.PARAM_ERROR.getMessage());
        }
        ShipperOrderDetailResponse orderDetail = lltorderMapper.getShipperOrderDetailByOrderId(orderId);
        if (!orderDetail.getTradeDriverState().equals(TradeCodeEnum.TO_LOADING_PLACE.getCode()) && !orderDetail.getTradeDriverState().equals(TradeCodeEnum.ARRIVAL_AT_LOADING_PLACE.getCode())) {
            log.info("司机已装货后货主不可主动取消订单");
            return Result.fail(false, ResultCodeEnum.FAIL.getCode(), "司机已装货");
        }
        return systemOrderService.cancelOrderWithoutPullOffCargo(orderId, OpsPersonTypeEnum.SHIPPER.getCode(), shipperId, OrderCancelReasonCodeEnum.SHIPPER_NEED_CHANGE_DRIVER.getCode(), OrderCancelReasonCodeEnum.SHIPPER_NEED_CHANGE_DRIVER.getMessage());
    }

    @Override
    public Result<Boolean> cancelOrderWithPullOffCargo(Long orderId, Long shipperId, Integer orderCancelReason, String orderCancelDescription) throws Exception {
        if (orderId == null) {
            return Result.fail(false, ResultCodeEnum.PARAM_ERROR);
        }
        ShipperOrderDetailResponse orderDetail = lltorderMapper.getShipperOrderDetailByOrderId(orderId);
        if (!orderDetail.getTradeDriverState().equals(TradeCodeEnum.TO_LOADING_PLACE.getCode()) && !orderDetail.getTradeDriverState().equals(TradeCodeEnum.ARRIVAL_AT_LOADING_PLACE.getCode())) {
            log.info("司机已装货后货主不可主动取消订单");
            return Result.fail(false, ResultCodeEnum.FAIL.getCode(), "司机已装货");
        }
        return systemOrderService.cancelOrderWithPullOffCargo(orderId, OpsPersonTypeEnum.SHIPPER.getCode(), shipperId, orderCancelReason, orderCancelDescription);
    }

    @Override
    public Result<Boolean> confirmReceipt(Long orderId, Long shipperId) throws Exception {
        if (orderId == null) {
            return Result.fail(false, ResultCodeEnum.PARAM_ERROR);
        }
        return systemOrderService.confirmReceipt(orderId, OpsPersonTypeEnum.SHIPPER.getCode(), shipperId);
    }

    @Override
    public Result<ShipperOrderDetailResponse> getOrderDetailByOrderId(Long orderId) {
        if (orderId == null) {

            return Result.fail(null, ResultCodeEnum.PARAM_ERROR.getCode(), ResultCodeEnum.PARAM_ERROR.getMessage());
        }
        ShipperOrderDetailResponse shipperOrderDetail = lltorderMapper.getShipperOrderDetailByOrderId(orderId);
        if (shipperOrderDetail == null) {
            return Result.fail(null, ResultCodeEnum.FAIL.getCode(), "无此订单");
        }
        System.out.println(shipperOrderDetail);
        BigInteger driverId = lltorderMapper.getDriverId(orderId);
        com.ymm.driver.api.utils.Result<DriverEntity> driverInfo = driverRelatedRepo.getDriverInfo(driverId);
        shipperOrderDetail.setDriverFirstName(driverInfo.getData().getDriverName());
        shipperOrderDetail.setDriverCarNum(driverInfo.getData().getDriverCarNum());
        return Result.success(shipperOrderDetail);
    }

    @Override
    public Result<List<ShipperOrderBriefResponse>> listOngoingOrdersByShipperId(Long shipperId, Integer orderShipperState) {
        if (shipperId == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR.getCode(), ResultCodeEnum.PARAM_ERROR.getMessage());
        }
        if (orderShipperState.equals(OrderStatusEnum.ONGOING.getCode())) {
            List<ShipperOrderBriefResponse> shipperOrderBriefResponses = lltorderMapper.listShipperOrderBriefsByShipperIdAndOrderShipperState(shipperId, orderShipperState);
            return Result.success(shipperOrderBriefResponses);
        }
        return Result.fail();
    }

    @Override
    public Result<List<ShipperOrderBriefResponse>> listCompletedOrdersByShipperId(Long shipperId, Integer orderShipperState) {
        if (shipperId == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR.getCode(), ResultCodeEnum.PARAM_ERROR.getMessage());
        }
        if (orderShipperState.equals(OrderStatusEnum.COMPLETED.getCode())) {
            List<ShipperOrderBriefResponse> shipperOrderBriefResponses =
                    lltorderMapper.listShipperOrderBriefsByShipperIdAndOrderShipperState(shipperId, orderShipperState);
            return Result.success(shipperOrderBriefResponses);
        }
        return Result.fail();
    }

    @Override
    public Result<List<ShipperOrderBriefResponse>> listCancelledOrdersByShipperId(Long shipperId, Integer orderShipperState) {
        if (shipperId == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR.getCode(), ResultCodeEnum.PARAM_ERROR.getMessage());
        }
        if (orderShipperState.equals(OrderStatusEnum.CANCELLED.getCode())) {
            List<ShipperOrderBriefResponse> shipperOrderBriefResponses = lltorderMapper.listShipperOrderBriefsByShipperIdAndOrderShipperState(shipperId, orderShipperState);
            return Result.success(shipperOrderBriefResponses);
        }

        return Result.fail();
    }


    @Override
    public Result<String> getDriverPhoneNumber(Long orderId) {
        if (orderId == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR.getCode(), ResultCodeEnum.PARAM_ERROR.getMessage());
        }
        BigInteger driverId = lltorderMapper.getDriverId(orderId);
        com.ymm.driver.api.utils.Result<DriverEntity> driverInfo = driverRelatedRepo.getDriverInfo(driverId);
        String driverPhone = driverInfo.getData().getDriverPhone();
        return Result.success(driverPhone);
    }

    /**
     * 获取指定收货人手机号的所有订单详情
     *
     * @param cargoReceiverPhone 收货人手机号
     * @return 所有订单详情
     */
    @Override
    public Result<List<ShipperOrderDetailResponse>> listOrderDetailByReceiverPhone(String cargoReceiverPhone) {
        if (cargoReceiverPhone == null) {
            return Result.fail(null, ResultCodeEnum.PARAM_ERROR);
        }
        List<ShipperOrderDetailResponse> shipperOrderDetails = lltorderMapper.getShipperOrderDetailByReceiverPhone(cargoReceiverPhone);
        if (shipperOrderDetails.size() < 1) {
            return Result.success(null);
        }
        BigInteger driverId = lltorderMapper.getDriverId(shipperOrderDetails.get(0).getOrderId());
        com.ymm.driver.api.utils.Result<DriverEntity> driverInfo = driverRelatedRepo.getDriverInfo(driverId);
        for (ShipperOrderDetailResponse shipperOrderDetail : shipperOrderDetails) {
            shipperOrderDetail.setDriverFirstName(driverInfo.getData().getDriverName());
            shipperOrderDetail.setDriverCarNum(driverInfo.getData().getDriverCarNum());
        }
        return Result.success(shipperOrderDetails);
    }

    @Override
    public Result<OrderAndCargoInfoResponse> getOrderAndCargoInfoByOrderId(Long orderId) {
        OrderAndCargoInfoResponse orderAndCargoInfoResponse = lltorderMapper.getOrderAndCargoInfoByOrderId(orderId);
        return Result.success(orderAndCargoInfoResponse);
    }
}
